import {
	getToken
} from './auth.js'
export var baseUrl = 'https://api.myonewow.com'
// export var baseUrl = 'http://192.168.11.89:82'
// export var newbaseUrl = 'http://192.168.11.89:82'
export var newbaseUrl = 'https://app.myonewow.com'
const config = {
	timeout: 60000, // 超时时间
	header: {}, // 请求头
}

// 响应拦截，对返回的数据进行预处理
// const responseInterceptors = function(res) {
// 	const data = res.data
// 	// 错误处理
// 	if (data && data.code === 200) {
// 		return {
// 			status: true,
// 			statusCode: data.code,
// 			data: data
// 		}
// 	}
// 	return {
// 		status: false,
// 		statusCode: data.code,
// 		message: "请求失败,请重试"
// 	}

// }


const responseInterceptors = function(res) {
	// 错误处理
	const data = res.data
	if (data.code === 0 || res.data.code == 200) {
		return {
			status: true,
			statusCode: data.code,
			data: data
		}
	}
	let token = uni.getStorageSync('token')
	if (res.data.msg == '登录过期') {
		setTimeout(() => {
			uni.reLaunch({
				url: '/pages/index/login/login'
			})
		},1000)
		
	}
	return {
		status: false,
		statusCode: data.code,
		message: `${res.data.msg ? res.data.msg : this.$t('requstEorr')}`
	}
}

// 请求拦截，在发起请求之前，进行配置设置
const requestInterceptors = function() {

	// 网络状态判断
	return new Promise(resolve => {
		uni.getNetworkType({
			success: async function(res) {
				if (res.networkType === 'none') {
					return resolve(false)
				} else {
					let token = uni.getStorageSync('token')
					config.header['Authorization'] = "Bearer " + token
					config.header['token'] = token
					// config.header['content-type'] = 'multipart/form-data'  
					return resolve(config)
				}
			},
			fail: function() {
				// 不常见的错误，当做网络问题处理
				return resolve(false)
			}
		})
	})
}

const request = function(method, path, data,newUrl) {
	return new Promise(async resolve => {
		const conf = await requestInterceptors()
		if (!conf) {
			uni.hideLoading()
			// 网络错误
			return resolve({
				status: false,
				message: 'network error'
			})
		}  
		uni.request({
			method,
			url:newUrl == 0 ? `${newbaseUrl}${path}` : `${baseUrl}${path}`,
			data: data,
			...conf,
			success: (res) => {
				uni.hideLoading()
				const data = responseInterceptors(res)
				console.log(data);
				return resolve(data)
			},
			fail: (err) => {
				uni.hideLoading()
				return resolve({
					status: false,
					message: 'Server error'
				})
			}
		})
	})
}


/**    
 * GET 请求
 * @param {Object} path ，请求地址
 * @param {Object} data ，传入的参数
 * @return {Promise(Object|false)}
 */
export function get(path, data,newUrl) {
	console.log(newUrl,'newUrl');
	return request('GET', path, data,newUrl)
}
/**
 * GET 请求
 * @param {Object} path ，请求地址
 * @param {Object} data ，传入的参数
 * @return {Promise(Object|false)}
 */
export function get2(path, data, loading) {
	if (loading === true) {
		uni.showLoading({
			title: "加载中...",
			mask: true
		});
	}
	return request('GET', path, data)
}
/**
 * POST 请求
 * @param {Object} path ，请求地址
 * @param {Object} data ，传入的参数
 * @return {Promise(Object|false)}
 */
export function post(path, data,newUrl) {
	return request('POST', path, data,newUrl)
}

/**
 * PUT 请求
 * @param {Object} path ，请求地址
 * @param {Object} data ，传入的参数
 * @return {Promise(Object|false)}
 */
export function put(path, data) {
	return request('PUT', path, data)
}

/**
 * DELETE 请求
 * @param {Object} path ，请求地址
 * @param {Object} data ，传入的参数
 * @return {Promise(Object|false)}
 */
export function del(path, data) {
	return request('DELETE', path, data)
}
// 分装页面跳转
export function next(url, d) {
	let data = JSON.stringify(d)
	uni.navigateTo({
		url: `${url}?data=${data}`
	})
}

export function next1(url, d) {
	let data = JSON.stringify(d)
	uni.reLaunch({
		url: `${url}?data=${data}`
	})
}